package user

import (
	"gorm.io/gorm"
	"publiclib/dbutil"
	"time"
)

type UserEntity struct {
	tx             *gorm.DB
	ID             int64  `gorm:"column:id;default:(-)"`
	UserId         string `gorm:"column:userId;default:(-)"`
	UName          string `gorm:"column:uname;default:(-)"`
	Pw             string
	NickName       string `gorm:"column:nickname;default:(-)" json:"nickname" form:"nickname"`
	Status         uint8
	Registertime   time.Time `gorm:"column:registertime;default:(-)"`
	Source         uint8
	IsVip          uint8  `gorm:"column:isVip;default:(-)"`
	ProvinceName   string `gorm:"column:provinceName;default:(-)"`
	Sex            *uint8 `gorm:"column:sex"`
	TotalSignIn    int64  `gorm:"column:totalSignIn;default:(-)"`    // 今年累计登录日期
	ConsecutiveDay int64  `gorm:"column:consecutiveDay;default:(-)"` // 今天连续登录天数
	WechatOpenId   string `gorm:"column:wechatOpenId;default:(-)"`   // 微信openId
	BindWechat     int    `gorm:"column:bindWechat;default:(-)"`
}

func (m *UserEntity) TableName() string {
	return "account"
}

type DBinfo struct {
	UserId string `gorm:"column:userId"`
	PW     string `gorm:"column:pw"`
}

func (m *UserEntity) Test(phone string) (*DBinfo, error) {
	var result DBinfo
	db := dbutil.GetDBCluster().GetDB()
	tx := db.Model(m).Select("userId,pw").Where("phoneNum=?", phone).Scan(&result)
	if tx.Error != nil {
		return nil, tx.Error
	}

	var count int
	err := db.Transaction(func(tx *gorm.DB) error {
		tmpTx := tx.Table("account").Select("COUNT(id)").Where("phoneNum=?", phone).Scan(&count)
		if tmpTx.Error != nil {
			return tmpTx.Error
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	return &result, nil
}
